<template>
  <div class="p-2">
    <el-card shadow="hover">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-cascader v-model="zskData" :options="options" @change="getList"/>
          </el-col>
          <el-col :span="1.5" v-if="zskData">
            <el-button  type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
          </el-col>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="noticeList">
        <el-table-column label="名称" align="center" prop="name" />
        <el-table-column label="图标" align="center" prop="icon" >
            <template #default="scope">
                <svg-icon :icon-class="scope.row.icon"></svg-icon>
            </template>
        </el-table-column>
        <el-table-column label="关联的文件夹名称" align="center" prop="fileName" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button v-hasPermi="['system:notice:edit']" link type="primary" icon="Edit" @click="handleUpdate(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button v-hasPermi="['system:notice:remove']" link type="primary" icon="Delete" @click="handleDelete(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
    <!-- 添加或修改公告对话框 -->
    <el-dialog v-model="dialog.visible" :title="dialog.title" width="600px" append-to-body>
      <el-form ref="noticeFormRef" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="标签图标" prop="icon">
              <!-- 图标选择器 -->
              <icon-select v-model="form.icon" />
            </el-form-item>
          </el-col>
          <el-col :span="24" >
            <el-form-item label="关联文件夹" prop="fileId">
                <el-tag type="primary" v-if="data.form.fileName" style="margin-right: 10px">{{ data.form.fileName }}</el-tag>
              <treeWj ref="treeWjRefs" :parentId="zskData[1]" @sureClick="sureClick"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
  
  <script setup name="Notice" lang="ts">
import { listNotice, getNotice, delNotice, addNotice, updateNotice } from '@/api/system/notice';
import { NoticeForm, NoticeQuery, NoticeVO } from '@/api/system/notice/types';
///// 自定义组建
import treeWj from "./component/copymove-tree.vue"
/// api
import { apiEnterpriseKnowledgeList,apiSysPortalconfigureAdd,apiSysPortalconfigureGetList ,apiSysPortalconfigureUpdata,apiSysPortalconfigureDel} from '@/api/enterprise/enterpriseConfig/index';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_notice_status, sys_notice_type } = toRefs<any>(proxy?.useDict('sys_notice_status', 'sys_notice_type'));

const noticeList = ref<NoticeVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);

const total = ref(0);
////////// / // / // / / / / /  选择知识库
const zskData = ref('');
const options = ref<any[]>([]);
const queryFormRef = ref<ElFormInstance>();
const noticeFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData = {
  name: '',
  icon: '',
  fileId: '',
  fileName: '',
  libraryId: '',
  id: null
};
const data = reactive({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    libraryId: null
  },
  rules: {
    name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
    icon: [{ required: true, message: '图标不能为空', trigger: 'change' }],
    fileId: [{ required: true, message: '关联文件夹不能为空', trigger: 'change' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询公告列表 */
const getList = async () => {
  loading.value = true;
  data.queryParams.libraryId = zskData.value[1];
  const res = await apiSysPortalconfigureGetList(queryParams.value);
  noticeList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};
/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};
/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  noticeFormRef.value?.resetFields();
};
/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加配置';
};
/**修改按钮操作 */
const handleUpdate = async (row?: any) => {
  reset();
  data.form.name = row.name
  data.form.icon = row.icon
  data.form.fileId = row.fileId
  data.form.fileName = row.fileName;
  data.form.libraryId = row.libraryId
  data.form.id = row.id
  dialog.visible = true;
  dialog.title = '修改配置';
};
/** 提交按钮 */
const submitForm = () => {
  noticeFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      form.value.id ? await apiSysPortalconfigureUpdata(form.value) : await apiSysPortalconfigureAdd(form.value);
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};
/** 删除按钮操作 */
const handleDelete = async (row?:any) => {
  await proxy?.$modal.confirm('是否确认删除名称为"' + row.name + '"的数据项？');
  await apiSysPortalconfigureDel([row.id]);
  await getList();
  proxy?.$modal.msgSuccess('删除成功');
};
/////////////////////////////////////////////////////////////   wjy ////
const getInit = async () => {
  let res: any = await apiEnterpriseKnowledgeList();
  if (res.code == 200) {
    options.value = res.data.map((item:any,index:number) => {
        return {
          value: index,
          label: item.classifyName,
          children: item.tree.map(item1 =>{
            return{
                value: item1.id,
                label: item1.realName,
            }
          })
        }
    });
  }
};
const sureClick = (row:any) =>{
    data.form.fileId = row.id;
    data.form.fileName = row.realName;
    data.form.libraryId = zskData.value[1];
}
onMounted(() => {
    getList();
  getInit();
});
</script>
  